package com.lujianfei.productprice.ui.ShopDetail;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lujianfei.productprice.R;
import com.lujianfei.productprice.base.BaseActivity;
import com.lujianfei.productprice.base.CommonAdapter;
import com.lujianfei.productprice.base.ViewHolder;
import com.lujianfei.productprice.ui.addProduct.ProductEntity;
import com.lujianfei.productprice.widget.BottomCart;
import com.lujianfei.productprice.widget.BottomTotalCount;

import java.util.ArrayList;
import java.util.List;

public class ShopActivity extends BaseActivity implements ShopContract.View{

    private ShopContract.Presenter presenter;
    private List<ProductEntity> productEntities = new ArrayList<>();
    private BaseAdapter baseAdapter;
    private ListView listview;
    private BottomCart bottomCart;
    private BottomTotalCount bottomTotalCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        setTitleMiddleText(getIntent().getStringExtra("name"));
        showTitleRightImage();
        setTitleRightImage(R.drawable.ic_add);
        listview = findViewById(R.id.listview);
        bottomCart = findViewById(R.id.bottom_cart);
        presenter = new ShopPresenter(this);
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ProductEntity productEntity = (ProductEntity) parent.getItemAtPosition(position);
                presenter.showLongClickDialog(productEntity);
                return true;
            }
        });
        listview.addFooterView(bottomTotalCount = new BottomTotalCount(this),null,false);
    }


    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadData();
        bottomCart.refreshQuantity();
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        presenter.openAddProduct();
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public long getId() {
        return getIntent().getLongExtra("id",-1);
    }

    @Override
    public void setData(List<ProductEntity> productEntities) {
        if (null == baseAdapter){
            baseAdapter = new CommonAdapter<ProductEntity>(this,this.productEntities,R.layout.adapter_shop_item) {

                @Override
                public void convert(ViewHolder viewHolder, final ProductEntity data) {
                    viewHolder.setText(R.id.txtProductName,data.getProductName());
                    viewHolder.setText(R.id.txtProductPrice,String.format("¥ %.2f",data.getPrice()));
                    ImageView iv_add = viewHolder.getView(R.id.iv_add);
                    iv_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            presenter.addToCart(data);
                        }
                    });
                }
            };
            listview.setAdapter(baseAdapter);
        }
        this.productEntities.clear();
        this.productEntities.addAll(productEntities);
        baseAdapter.notifyDataSetChanged();
        bottomTotalCount.setTotalCount(this.productEntities.size());
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public String getShopName() {
        return getIntent().getStringExtra("name");
    }

    @Override
    public void refreshQuantity() {
        bottomCart.refreshQuantity();
    }
}
